package com.fengxu.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.fengxu.springcloud.entity.CommonResult;
import com.fengxu.springcloud.entity.Payment;
import com.fengxu.springcloud.myhandler.CustomerBlockHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RateLimitController {


    @GetMapping("/byResource")
    @SentinelResource(value = "byResource", blockHandler = "handlerException")
    public CommonResult byResource(){

        return new CommonResult(200,"按资源名限流资源测试",
                new Payment(2021L,"serial001"));
    }

    public CommonResult handlerException(BlockException e){
        return new CommonResult(400,e.getClass().getCanonicalName(),"服务不可用\t");
    }

    @GetMapping("/byUrl")
    @SentinelResource(value = "byUrl",
        blockHandlerClass = CustomerBlockHandler.class,
        blockHandler = "handlerException2")
    public CommonResult byUrl(){
        return new CommonResult(200,"按url限流测试！",
                new Payment(2021L, "serial002"));
    }

}
